<template>
  <div class="AticleList-wrap">
    <van-cell>全部评论</van-cell>
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
    <articleItem :list="list" @handleIsReply="$emit('handleIsReply',$event)"/>
    </van-list>
  </div>
</template>
<script>
import { getComments } from '../../../api/following'
import articleItem from './article-itme.vue'
export default {
  name: 'AticleList',
  data () {
    return {
      loading: false,
      finished: false,
      offset: null,
      limit: 10
    }
  },
  components: {
    articleItem
  },
  props: {
    id: {
      type: [Number, Object, String],
      required: true
    },
    type: {
      type: String,
      default: 'a'
    },
    list: {
      type: Array,
      default: () => []// 对象和数组的默认值需要是一个函数返回值
    }

  },
  mounted () {},
  methods: {
    async onLoad () {
      // 获取数据
      const params = {
        type: this.type, // 评论类型，a-对文章(article)的评论，c-对评论(comment)的回复
        source: this.id.toString(), // 源id，文章id或评论id
        offset: this.offset, // 获取评论数据的偏移量，值为评论id，表示从此id的数据向后取，不传表示从第一页开始读取数据
        limit: this.limit // 获取的评论数据个数，不传表示采用后端服务设定的默认每页数据量
      }
      const { data } = await getComments(params)

      this.$emit('handleTotalCount', data.data.total_count)
      // 将数据放进list
      const body = data.data.results
      this.list.push(...body)

      // 加载状态结束
      this.loading = false

      // 判断数据全部加载完成
      if (body.length) {
        // 返回的数组有值 下次继续加载
        this.offset = data.data.last_id
      } else {
        // 否则停止加载
        this.finished = true
      }
    }
  }
}
</script>
<style scoped lang='less'>

</style>
